package com.yt.setclaim.server.api.cms;

import com.yt.common.constant.Result;
import com.yt.setclaim.models.entity.MedicalReview;
import com.yt.setclaim.models.service.MedicalReviewService;
import com.yt.setclaim.models.vo.MedicalReviewVO;
import com.yt.setclaim.models.vo.ClaimInfoVO;
import com.yt.setclaim.models.dto.MedicalReviewDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cms/setclaim-medical-review")
public class MedicalReviewController {

    @Autowired
    private MedicalReviewService medicalReviewService;

    /**
     * 获取所有已审核的医疗审核记录
     */
    @GetMapping("/list")
    public Result<List<MedicalReviewVO>> list() {
        List<MedicalReview> list = medicalReviewService.list();
        List<MedicalReviewVO> voList = list.stream().map(medicalReview -> {
            MedicalReviewVO vo = new MedicalReviewVO();
            BeanUtils.copyProperties(medicalReview, vo);
            // ID转换为字符串类型
            vo.setReviewId(String.valueOf(medicalReview.getReviewId()));
            vo.setClaimId(String.valueOf(medicalReview.getClaimId()));
            vo.setAdminId(String.valueOf(medicalReview.getAdminId()));
            return vo;
        }).collect(Collectors.toList());
        return Result.success(voList);
    }

    /**
     * 获取待审核的索赔记录（从索赔信息表中获取未审核的索赔记录）
     */
    @GetMapping("/pending-list")
    public Result<List<ClaimInfoVO>> getPendingClaims() {
        // 这里需要调用索赔服务获取待审核的索赔记录
        // 由于索赔模块不在当前项目中，这里只是示例
        return Result.success(medicalReviewService.getPendingClaims());
    }

    /**
     * 根据ID获取医疗审核记录
     */
    @GetMapping("/info/{id}")
    public Result<MedicalReviewVO> info(@PathVariable("id") Integer id) {
        MedicalReview medicalReview = medicalReviewService.getById(id);
        MedicalReviewVO vo = new MedicalReviewVO();
        BeanUtils.copyProperties(medicalReview, vo);
        // ID转换为字符串类型
        vo.setReviewId(String.valueOf(medicalReview.getReviewId()));
        vo.setClaimId(String.valueOf(medicalReview.getClaimId()));
        vo.setAdminId(String.valueOf(medicalReview.getAdminId()));
        return Result.success(vo);
    }

    /**
     * 根据索赔ID获取医疗审核记录
     */
    @GetMapping("/info-by-claim/{claimId}")
    public Result<MedicalReviewVO> infoByClaimId(@PathVariable("claimId") Integer claimId) {
        MedicalReview medicalReview = medicalReviewService.getByClaimId(claimId);
        if (medicalReview == null) {
            return Result.fail("未找到对应的医疗审核记录");
        }
        MedicalReviewVO vo = new MedicalReviewVO();
        BeanUtils.copyProperties(medicalReview, vo);
        // ID转换为字符串类型
        vo.setReviewId(String.valueOf(medicalReview.getReviewId()));
        vo.setClaimId(String.valueOf(medicalReview.getClaimId()));
        vo.setAdminId(String.valueOf(medicalReview.getAdminId()));
        return Result.success(vo);
    }

    /**
     * 添加医疗审核记录（审核功能）
     */
    @PostMapping("/review")
    public Result<String> review(@RequestBody MedicalReviewDTO medicalReviewDTO) {
        // 校验管理员ID是否存在
        if (!medicalReviewService.isValidAdminId(Integer.valueOf(medicalReviewDTO.getAdminId()))) {
            return Result.fail("管理员ID不存在，请选择有效的管理员");
        }
        
        MedicalReview medicalReview = new MedicalReview();
        BeanUtils.copyProperties(medicalReviewDTO, medicalReview);
        // ID转换为Integer类型
        if (medicalReviewDTO.getReviewId() != null) {
            medicalReview.setReviewId(Integer.valueOf(medicalReviewDTO.getReviewId()));
        }
        if (medicalReviewDTO.getClaimId() != null) {
            medicalReview.setClaimId(Integer.valueOf(medicalReviewDTO.getClaimId()));
        }
        if (medicalReviewDTO.getAdminId() != null) {
            medicalReview.setAdminId(Integer.valueOf(medicalReviewDTO.getAdminId()));
        }
        medicalReviewService.save(medicalReview);
        return Result.success("审核成功");
    }

    /**
     * 修改医疗审核记录
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody MedicalReviewDTO medicalReviewDTO) {
        // 校验管理员ID是否存在
        if (!medicalReviewService.isValidAdminId(Integer.valueOf(medicalReviewDTO.getAdminId()))) {
            return Result.fail("管理员ID不存在，请选择有效的管理员");
        }
        
        MedicalReview medicalReview = new MedicalReview();
        BeanUtils.copyProperties(medicalReviewDTO, medicalReview);
        // ID转换为Integer类型
        if (medicalReviewDTO.getReviewId() != null) {
            medicalReview.setReviewId(Integer.valueOf(medicalReviewDTO.getReviewId()));
        }
        if (medicalReviewDTO.getClaimId() != null) {
            medicalReview.setClaimId(Integer.valueOf(medicalReviewDTO.getClaimId()));
        }
        if (medicalReviewDTO.getAdminId() != null) {
            medicalReview.setAdminId(Integer.valueOf(medicalReviewDTO.getAdminId()));
        }
        medicalReviewService.updateById(medicalReview);
        return Result.success("修改成功");
    }

    /**
     * 删除医疗审核记录
     */
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody MedicalReviewDTO medicalReviewDTO) {
        if (medicalReviewDTO.getReviewId() == null || medicalReviewDTO.getReviewId().isEmpty()) {
            return Result.fail("审核记录ID不能为空");
        }
        
        try {
            Integer reviewId = Integer.valueOf(medicalReviewDTO.getReviewId());
            medicalReviewService.removeById(reviewId);
            return Result.success("删除成功");
        } catch (NumberFormatException e) {
            return Result.fail("审核记录ID格式不正确");
        }
    }
}